﻿using System.Collections;
using Assets.AnimationPlayer.Scripts.Animations;
using Assets.AnimationPlayer.Scripts.Prefabs;
using Assets.AnimationPlayer.Scripts.Util;
using UnityEngine;
using UnityEngine.UI;

namespace Assets.AnimationPlayer.Scripts.UI
{
    public class AnimationItem : MonoBehaviour
    {
        public Text Title;
        public RawImage Image;

        private AnimationObj _animationObj;
        public int Index;
        public bool IsDetailView = false;


        // Use this for initialization
        internal void Start()
        {
            if(!IsDetailView)
                 YioksTriggerListener.Get(transform).OnClick = _playAnimation;
        }

        private void _playAnimation(GameObject go)
        {
            if (!IsDetailView)
            {
                if (_pMyScrollRect != null && _pMyScrollRect.IsScrolling) return;
                if (Index != -1)
                {
                    AnimationVar.AnimationPlayCfg.SelectAnimationIndex = Index;
                    AnimationVar.PlayAnimation();
                }
            }

        }


        private MyScrollRect _pMyScrollRect;
        public void SetItem(AnimationObj obj, int index, MyScrollRect pMyScrollRect)
        {
            _animationObj = obj;
            Index = index;
            _pMyScrollRect = pMyScrollRect;

            if (IsDetailView)
            {
                Title.text = _animationObj.AnimationName;
            }
            else
            {
                Title.text = "<color=red>" + (index + 1) + ".</color>" + _animationObj.AnimationName;
            }

            //image
            StartCoroutine(_loadImage(_animationObj.ImageUrl, "Image_" + Index));

        }

        private IEnumerator _loadImage(string imagePath, string cacheKey = "")
        {
            if (!string.IsNullOrEmpty(cacheKey) && AnimationVar.AnimationPlayCfg.ImageCacheDictionary.ContainsKey(cacheKey))
            {
                var image = Image.GetComponent<RawImage>();
                image.texture = AnimationVar.AnimationPlayCfg.ImageCacheDictionary[cacheKey];
                YioksTool.SetAlpha(image, 1.0f);
            }
            else
            {
                var request = Resources.LoadAsync<Texture>("images/" + imagePath.Replace(".png", "").Replace(".png", ""));
                yield return request;
                if (request.isDone && request.asset != null)
                {
                    var imageTexture2D = Instantiate(request.asset as Texture2D);
                    if (!string.IsNullOrEmpty(cacheKey))
                    {
                        if (AnimationVar.AnimationPlayCfg.ImageCacheDictionary.ContainsKey(cacheKey))
                        {
                            //AnimationVar.ImageCacheDictionary[cacheKey] = imageTexture2D;
                        }
                        else
                        {
                            AnimationVar.AnimationPlayCfg.ImageCacheDictionary.Add(cacheKey, imageTexture2D);
                        }

                    }
                    var image = Image.GetComponent<RawImage>();
                    image.texture = imageTexture2D;
                    YioksTool.SetAlpha(image, 1.0f);
                    yield return new WaitForEndOfFrame();
                }
            }
        }
    }
}
